'''
Author: ironlionwgHH huanghao_bnu@163.com
Date: 2023-05-22 16:11:45
LastEditors: ironlionwgHH huanghao_bnu@163.com
LastEditTime: 2023-05-22 22:27:54
FilePath: /asteroid_orbit/src/_oop_file_process.py
Description: 对oop文件进行操作
'''
'''
 _   _                                     _   _                   _          ____               _
| | | |  _   _    __ _   _ __     __ _    | | | |   __ _    ___   ( )___     / ___|   ___     __| |   ___ 
| |_| | | | | |  / _` | | '_ \   / _` |   | |_| |  / _` |  / _ \  |// __|   | |      / _ \   / _` |  / _ \
|  _  | | |_| | | (_| | | | | | | (_| |   |  _  | | (_| | | (_) |   \__ \   | |___  | (_) | | (_| | |  __/
|_| |_|  \__,_|  \__,_| |_| |_|  \__, |   |_| |_|  \__,_|  \___/    |___/    \____|  \___/   \__,_|  \___|
                                  |___/
'''
import sys
import os

class oop_file():
    def __init__(self, oop_file_path: str, oop_file_name: str):
        self.oop_file_path = oop_file_path
        self.oop_file_name = oop_file_name
        self.oop_file_path_name = os.path.join(self.oop_file_path, self.oop_file_name)
        self.object_name = oop_file_name.split("_")[0]
        # 提取信息
        with open(self.oop_file_path_name, "r") as oop_file:
            oop_file_list = oop_file.readlines()
            self.oop_file_list_data = oop_file_list
    '''
    Function :
                change_param
    Description :
                Change parameter in self.oop_file_list_data
    Edition  :  Huang Hao/2023.05.22/
    Log      :
    Input    :
                param_name      str         参数名称 | e.g. '.epoch'
                param_value     str         参数数值 | e.g. ' CAL 2023/06/05 0:0:0 TDT'
    Return   :
                
    func used:
                
    Warning  :
                还没有考虑可能会有重复的参数名称,会出现一个参数中存在重复名字的部分，如.epoch和.epoch.start等，现在的解决办法是"="前后都应该有空格
    '''
    def change_param(self,  param_name: str, param_value: str) -> None:
        print('Warning:还没有考虑可能会有重复的参数名称,会出现一个参数中存在重复名字的部分，如.epoch和.epoch.start等，现在的解决办法是"="前后都应该有空格,见_oop_file_procsee.py')
        line_num = 0
        for line in self.oop_file_list_data:
            # 判断是不是参数设置行，可能为空行或者参数组的名称
            if "=" in line:
                line_param_name = line.split("=")[0]
                # 判断是需要修改的参数
                if param_name in line_param_name:
                    # 修改参数
                    new_line = "    %s = %s\n"%(param_name,param_value)
                    self.oop_file_list_data[line_num] = new_line
            line_num += 1
            
            pass
        pass
                    
    '''
    Function :
                generate_new_oop_file
    Description :
                Generate new oop file according to self.oop_file_list_data
    Edition  :  Huang Hao/2023.05.22/
    Log      :
    Input    :
                new_oop_file_path   str
                new_oop_file_name   str
    Return   :  
    func used:
    Warning  :
                
    '''
    def generate_new_oop_file(self, new_oop_file_path: str, new_oop_file_name: str) -> None:
        new_oop_file_path_name = os.path.join(new_oop_file_path, new_oop_file_name)
        with open(new_oop_file_path_name, "w") as new_oop_file:
            for line in self.oop_file_list_data:
                new_oop_file.write(line)
                pass
        pass
    
if __name__ == "__main__":
    HO3_oop_reference_file = oop_file(oop_file_path="../test/2016HO3/",
                                      oop_file_name="469219.oop.reference")
    HO3_oop_reference_file.change_param(param_name=".inc_files ",
                                        param_value="huanghao_test.oel")
    HO3_oop_reference_file.change_param(param_name=".epoch.start ",
                                        param_value="CAL TEST/01/01 00:00:00 UTC")
    HO3_oop_reference_file.generate_new_oop_file(new_oop_file_path="../test/2016HO3/",
                                                 new_oop_file_name="test_for_oop.new_oop")
    